define(['jquery','fullcalendar','fullcalendar_zh-cn','ajaxServ','formServ','btnServ','sysmanMain','msgServ','deptUserSelect','buttonEvent','datagridServ','bizProto'],
		function($,fullcalendar,lang,ajaxServ,formServ,btnServ,sysmanMain,msgServ,deptUserSelect,buttonEvent,datagridServ,bizProto){
		
		//var qryUrl='/'+sysmanMain.getRootPath()+'/oaDutyList/query/getDutyList.action';
		
		var window_id = 'panel-oa-dutyList';
		var funcDef = {
			
			refreshForm:function(data){
				var _form = $('#'+window_id +' form.main-form')
				if(_form.length>0){
					_form.form('clear');
					_form.form('load',data)
				}
			},
			btnClick:function(e){
				
				if(window.console){
					console.log('dutylist btn click');
				}
				if(e && e.currentTarget){
					
					
					var e2=e.delegateTarget
					
					
					var _type =$(e2).attr('btn-type');
					
					if(_type){
						switch(_type){
							case 'add':
							var _form = $('#'+window_id +' form.main-form');
							_form.form('clear');
							funcDef.initUser();
							break;
							case 'save':
							funcDef.saveFunction();
							break;
							case 'del':
							funcDef.delFunction();
							break;
							
						}
					}
				}
			},
			saveFunction:function(){
				var _form = $('#'+window_id +' form.main-form')
				var validateStatus =_form.form('enableValidation').form('validate');
				if(!validateStatus){
					msgServ.Msgslide("请填写表单内容");
					return
				}
				var data = _form.serializeArray();
				var _data = {};
				for (var name in data) {
					_data[data[name].name] = data[name].value;
				}

				var _param = {
					url :'/'+sysmanMain.getRootPath()+'/oaDutyList/save.action',
					dataType : 'json',
					data : $.toJSON(_data),
					type : 'post',
					contentType : "appliction/json;charset=utf-8",
					success:function(data){
						msgServ.Msgslide('操作成功！');
						$('#'+window_id +' form.main-form').form('load',data);
						$('#'+window_id +' #calendar-root').fullCalendar( 'refetchEvents');
					},
					error:function(){
						msgServ.Msgslide('操作失败！')
					}
				}
				$.ajax(_param)			
				
			},
			delFunction:function(){
				var _id = $('#'+window_id +' #id')
				if(_id.length==1 && _id.val()){
					$.messager.confirm('警告', '确定执行删除操作?', function(r){
						
						if( r){	
							var idv = _id.val();
							var _param = {
								url :'/'+sysmanMain.getRootPath()+'/oaDutyList/del.action',					
								data : {id:idv},
								type : 'post',					
								success:function(){
									$('#'+window_id +' #calendar-root').fullCalendar( 'removeEvents',idv);
									$('#'+window_id +' form.main-form').form('clear');
									msgServ.Msgslide('操作成功！')
								},
								error:function(){
									msgServ.Msgslide('操作失败！')
								}			
							
							}
							$.ajax(_param)
						}
					})
				}else{
					msgServ.Msgslide('请选择要删除的日程！')
				}
			}
			,
			initUser:function(){
				//初始化用户
				var _usr =sysmanMain.getCurrentUser();
				var _el = $('#panel-oa-dutyList #apply_name')
				if(_el){
					_el.textbox('setValue',_usr.name)
					
				}
				
				_el = $('#panel-oa-dutyList #apply_id')
				if(_el){
					_el.val(_usr.id)
				}
				
				//初始化部门				
				_el = $('#panel-oa-dutyList #apply_deptname')
				if(_el){
					_el.textbox('setValue',_usr.deptName)
					
				}
				
				_el = $('#panel-oa-dutyList #apply_deptid')
				if(_el){
					_el.val(_usr.deptid)
				}	
			},
			//初始化calendar
			initCalendar:function(_options){
					$('#'+window_id +' #calendar-root').fullCalendar({
					header: {
						left: 'prev,next today',
						center: 'title',
						right: 'month,agendaWeek,agendaDay'
					},
					editable: false,
					eventLimit: true, // allow "more" link when too many events
					lang:'zh-cn',
				    height:540,
				    eventLimit: true,
				    theme:false,
				    events:_options,
			        eventClick: function(events, jsEvent, view) {
			        	if(events && events._id){
				        	var _param = {
								url :'/'+sysmanMain.getRootPath()+'/oaDutyList/query/getOne.action?id='+events._id,					
								type : 'post',
								contentType : "appliction/json;charset=utf-8",
								success:funcDef.refreshForm,
								error:function(){
									msgServ.Msgslide('操作失败！')
								}
							}
							$.ajax(_param)
						}			        	
					}				    
				});		
			}
		}

		var public_method = {
			init:function(){
				//var queryParam;
				var qryUrl='/'+sysmanMain.getRootPath()+'/oaDutyList/query/getDutyList.action';
				/*var _usr =sysmanMain.getCurrentUser();
				var _data;
				var _parm={
					url:qryUrl,
					data:{condition:{apply_id:_usr.id}},
					type:'post',
					contentType : "appliction/json;charset=utf-8",
					success:function(data){						
						console.log(data);
						//initCalendar(data);
					}
				}
				$.ajax(_parm);
				//console.log(data);
				*/
				$('#'+window_id +' #calendar-root').fullCalendar({
					header: {
						left: 'prev,next today',
						center: 'title',
						right: 'month,agendaWeek,agendaDay'
					},
					editable: false,
					eventLimit: true, // allow "more" link when too many events
					lang:'zh-cn',
				    height:540,
				    eventLimit: true,
				    theme:false,
				    events:qryUrl,
			        eventClick: function(events, jsEvent, view) {
			        	if(events && events._id){
				        	var _param = {
								url :'/'+sysmanMain.getRootPath()+'/oaDutyList/query/getOne.action?id='+events._id,					
								type : 'post',
								contentType : "appliction/json;charset=utf-8",
								success:funcDef.refreshForm,
								error:function(){
									msgServ.Msgslide('操作失败！')
								}
							}
							$.ajax(_param)
						}			        	
					}				    
				});		
				
				/*****绑定按钮函数*******/
			    var $btn= $("#" +window_id +" button.btn")
			    
			    if(window.console){
					console.log('length:'+$btn.length)
				}
				$btn.unbind('click');
				$btn.bind('click',funcDef.btnClick)	
				
				/******弹窗选择人员及部门********/
				var el = $('#panel-oa-dutyList #apply_name')
				console.log('el.length:' + el.length)
				$(el).textbox({
					onClickButton:function(data){								
						deptUserSelect.openPage({
							userids:window_id +' #apply_id',
							usernames:window_id +' #apply_name',
							deptid:window_id +' #apply_deptid',
							deptname:window_id +' #apply_deptname'					
						})								
					}
				})
				
				//初始化用户
				funcDef.initUser();
				$.messager.progress('close');	
			}
		}
		return public_method;
	});